package com.zh.mouldleapp.manager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Hashtable;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.bitmap.core.BitmapCommonUtils;
import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.zh.mouldleapp.R;

/**
 * **********************************************************<p>
 *  内容摘要	：全局应用程序类：用于保存和调用全局应用配置<p>
 *
 *  作者	：DongHuiWu<p>
 *  创建时间	：2013-3-14 下午5:53:39 <p>
 *  当前版本号：v1.0<p>
 *  历史记录	:<p>
 *  	日期	: 2013-3-14 下午5:53:39 	修改人：<p>
 *  	描述	:<p>
 ***********************************************************
 */
public class AppContext extends Application {

	private static final String TAG = "Application";
	public static final int NETTYPE_WIFI = 0x01;
	public static final int NETTYPE_CMWAP = 0x02;
	public static final int NETTYPE_CMNET = 0x03;
	public FinalBitmap finalBitmap;
	public FinalHttp finalHttp;
	public FinalDb finalDb;
	private Hashtable<String, Object> memCacheRegion = new Hashtable<String, Object>();
	public double RATE;
	public boolean toShoping = false;
	private String DB_PATH = "data/data/com.slw.app/databases/slw_app.db";// 数据库存储位置
	private String DB_NAME = "slw_app.db";// 数据库名
	public boolean isLogin;// APP是否有账号登录

	@Override
	public void onCreate() {
		super.onCreate();
		finalBitmap = FinalBitmap
				.create(getApplicationContext(), BitmapCommonUtils.getDiskCacheDir(getApplicationContext(), "hq").getAbsolutePath());
		System.out.println("BitmapCommonUtils.getDiskCacheDir(getApplicationContext(), .getAbsolutePath()==="+BitmapCommonUtils.getDiskCacheDir(getApplicationContext(), "hq").getAbsolutePath());
		finalBitmap.configLoadingImage(R.drawable.default_news);// 正在加载时显示的图片
		finalBitmap.configLoadfailImage(R.drawable.default_news);// 加载失败显示的图片
		finalHttp = new FinalHttp();
	}


	/**
	 * 检测网络是否可用
	 * @return
	 */
	public boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		return ni != null && ni.isConnectedOrConnecting();
	}


	/**
	 * 将对象保存到内存缓存中
	 * @param key
	 * @param value
	 */
	public void setMemCache(String key, Object value) {
		memCacheRegion.put(key, value);
	}

	/**
	 * 从内存缓存中获取对象
	 * @param key
	 * @return
	 */
	public Object getMemCache(String key) {
		return memCacheRegion.get(key);
	}

	/**
	 * 保存对象
	 * @param ser
	 * @param file
	 * @throws IOException
	 */
	public boolean saveObject(Serializable ser, String file) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = openFileOutput(file, MODE_PRIVATE);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(ser);
			oos.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				oos.close();
			} catch (Exception e) {
			}
			try {
				fos.close();
			} catch (Exception e) {
			}
		}
	}

	/**
	 * 判断缓存是否存在
	 * @param cachefile
	 * @return
	 */
	private boolean isExistDataCache(String cachefile) {
		boolean exist = false;
		File data = getFileStreamPath(cachefile);
		if (data.exists())
			exist = true;
		return exist;
	}








}